home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / RegionToRectangles Folder / RegionToRectangles.c < prev   
Text File  |  1994-02-19  |  4KB  |  223 lines

  1.  
  2. /*    Hugh Fisher 1993
  3.  
  4.     Demo program to show off EachRegionRect code.
  5.     
  6.     Generates a series of regions and then redraws
  7.     them using the function. Click to move on to
  8.     the next.
  9.                                                     */
  10.  
  11. #include <stdio.h>
  12.  
  13. WindowPtr    window;
  14.  
  15. #define ArraySize(array) (sizeof((array))/sizeof((array)[0]))
  16.  
  17. extern void EachRegionRect (RgnHandle r, void (* proc)(Rect *));
  18.  
  19. void initMac (void);
  20.  
  21. void makeRegion (RgnHandle * rgn, Point points[], short npoints);
  22. void makeDisconnected (RgnHandle * rgn);
  23. void makeHole (RgnHandle * rgn);
  24.  
  25. void waitClick (void);
  26. void slowMotion (Rect * r);
  27. void showRegion (RgnHandle rgn);
  28.  
  29. static Point square[] = {
  30.     { 0, 0 },
  31.     { 96, 0 },
  32.     { 96, 96 },
  33.     { 0, 96 },
  34.     { 0, 0 }
  35.     };
  36.  
  37. static Point T_up[] = {
  38.     { 32, 0 },
  39.     { 64, 0 },
  40.     { 64, 48 },
  41.     { 96, 48 },
  42.     { 96, 96 },
  43.     { 0, 96 },
  44.     { 0, 48 },
  45.     { 32, 48 },
  46.     { 32, 0 }
  47.     };
  48.  
  49. static Point T_down[] = {
  50.     { 0, 0 },
  51.     { 96, 0 },
  52.     { 96, 48 },
  53.     { 64, 48 },
  54.     { 64, 96 },
  55.     { 32, 96 },
  56.     { 32, 48 },
  57.     { 0, 48 },
  58.     { 0, 0 }
  59.     };
  60.  
  61. static Point H_shape[] = {
  62.     { 0, 0 },
  63.     { 32, 0 },
  64.     { 32, 32 },
  65.     { 64, 32 },
  66.     { 64, 0 },
  67.     { 96, 0 },
  68.     { 96, 96 },
  69.     { 64, 96 },
  70.     { 64, 64 },
  71.     { 32, 64 },
  72.     { 32, 96 },
  73.     { 0, 96 },
  74.     { 0, 0 }
  75.     };
  76.  
  77. static Point I_shape[] = {
  78.     { 0, 0 },
  79.     { 96, 0 },
  80.     { 96, 32 },
  81.     { 64, 32 },
  82.     { 64, 64 },
  83.     { 96, 64 },
  84.     { 96, 96 },
  85.     { 0, 96 },
  86.     { 0, 64 },
  87.     { 32, 64 },
  88.     { 32, 32 },
  89.     { 0, 32 },
  90.     { 0, 0 }
  91.     };
  92.  
  93. void initMac ()
  94. {
  95.     /* Standard Toolbox incantation */
  96.     InitGraf(&thePort);
  97.     InitFonts();
  98.     InitWindows();
  99.     InitMenus();
  100.     TEInit();
  101.     InitDialogs(NULL);
  102.     SetEventMask(everyEvent);
  103.     FlushEvents(everyEvent, 0);
  104.     InitCursor();
  105. }
  106.     
  107. void makeRegion (RgnHandle * rgn, Point points[], short npoints)
  108. {
  109.     short    loop;
  110.     
  111.     *rgn = NewRgn();
  112.     OpenRgn();
  113.     MoveTo(points[0].v, points[0].h);
  114.     for (loop = 1; loop < npoints; loop ++)
  115.         LineTo(points[loop].v, points[loop].h);
  116.     CloseRgn(*rgn);
  117.     PaintRgn(*rgn);
  118. }
  119.  
  120. void makeDisconnected (RgnHandle * rgn)
  121. {
  122.     Rect    bounds;
  123.     
  124.     *rgn = NewRgn();
  125.     OpenRgn();
  126.     SetRect(&bounds, 0, 0, 32, 32);
  127.     FrameRect(&bounds);
  128.     SetRect(&bounds, 64, 64, 96, 96);
  129.     FrameRect(&bounds);
  130.     CloseRgn(*rgn);
  131.     PaintRgn(*rgn);
  132. }
  133.  
  134. void makeHole (RgnHandle * rgn)
  135. {
  136.     Rect    bounds;
  137.     
  138.     *rgn = NewRgn();
  139.     OpenRgn();
  140.     SetRect(&bounds, 0, 0, 32, 96);
  141.     FrameRect(&bounds);
  142.     SetRect(&bounds, 64, 0, 96, 96);
  143.     FrameRect(&bounds);
  144.     SetRect(&bounds, 32, 0, 64, 32);
  145.     FrameRect(&bounds);
  146.     SetRect(&bounds, 32, 64, 64, 96);
  147.     FrameRect(&bounds);
  148.     CloseRgn(*rgn);
  149.     PaintRgn(*rgn);
  150. }
  151.  
  152. void slowMotion (Rect * r)
  153. {
  154.     short        x, y;
  155.     long        now;
  156.     PenState    save;
  157.     
  158.     /* Use invert rather than normal draw to make
  159.        sure we aren't doing any pixels twice    */
  160.     GetPenState(&save);
  161.     PenMode(patXor);
  162.     for (x = r->left; x < r->right; x++) {
  163.         now = TickCount();
  164.         while (now == TickCount())
  165.             SystemTask();
  166.         MoveTo(x, r->top);
  167.         LineTo(x, r->bottom);
  168.     }
  169.     SetPenState(&save);
  170. }
  171.  
  172. void showRegion (RgnHandle rgn)
  173. {
  174.     long    discard;
  175.     
  176.     Delay(60, &discard);
  177.     EraseRect(&(**rgn).rgnBBox);
  178.     EachRegionRect(rgn, slowMotion);
  179.     Delay(60, &discard);
  180.     EraseRect(&window->portRect);
  181.  
  182. }
  183.  
  184. void main ()
  185. {
  186. #define kWidth 256
  187. #define kHeight 256
  188.  
  189.     Rect bounds;
  190.     RgnHandle rgn;
  191.     short    x, y;
  192.     
  193.     initMac();
  194.     
  195.     x = (screenBits.bounds.right - screenBits.bounds.left - kWidth) / 2;
  196.     y = GetMBarHeight() + 32;
  197.     SetRect(&bounds, x, y, x + kWidth, y + kHeight);
  198.     window = NewWindow(NULL, &bounds, "\pRegion to Rectangles", TRUE, noGrowDocProc, (WindowPtr)-1, FALSE, 0);
  199.     SetPort(window);
  200.     SetOrigin(-64, -64);
  201.     
  202.     makeRegion(&rgn, square, ArraySize(square));
  203.     showRegion(rgn);
  204.     
  205.     makeRegion(&rgn, T_up, ArraySize(T_up));
  206.     showRegion(rgn);
  207.     
  208.     makeRegion(&rgn, T_down, ArraySize(T_down));
  209.     showRegion(rgn);
  210.     
  211.     makeRegion(&rgn, H_shape, ArraySize(H_shape));
  212.     showRegion(rgn);
  213.     
  214.     makeRegion(&rgn, I_shape, ArraySize(I_shape));
  215.     showRegion(rgn);
  216.     
  217.     makeHole(&rgn);
  218.     showRegion(rgn);
  219.     
  220.     makeDisconnected(&rgn);
  221.     showRegion(rgn);
  222. }
  223.